import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const foodsReducer=createSlice({
    name:"foods",
    initialState:{
        foodsList:[],
        activeIndex:0,
        cartList:[]
    },
    reducers:{
        setFoodsList:(state,action)=>{
            state.foodsList=action.payload
        },
        changeActiveIndex:(state,action)=>{
            state.activeIndex=action.payload
        },
        addCart:(state,action)=>{
        const item= state.cartList.find(item=>item.id===action.payload.id);
        if(item){
            item.count++;
        }else{
            state.cartList.push(action.payload)
        }
        },
        incrementCount:(state,action)=>{
            const item= state.cartList.find(item=>item.id===action.payload);
            item.count++;
        },
        decrementCount:(state,action)=>{
            const item= state.cartList.find(item=>item.id===action.payload);
            if(item.count>0){
                item.count--;
                
            }
        },
        clearCart:(state)=>{
            state.cartList=[]
        }
    }
})
const {setFoodsList,changeActiveIndex,addCart,incrementCount,decrementCount,clearCart}=foodsReducer.actions
const fetchFoodsList=()=>{
    return async (dispatch)=>{
        const response=await axios.get("http://localhost:3004/takeaway")
        const data=response.data
        dispatch(setFoodsList(data ))
    }
}
export {fetchFoodsList,changeActiveIndex,addCart,incrementCount,decrementCount,clearCart}
export default foodsReducer.reducer